#ifndef COMPRESS_THREAD_H
#define COMPRESS_THREAD_H
#include <thread>
#include <atomic>
#include <vector>
#include <condition_variable>
#include <lz4.h>
#include <cstring>
#include <iostream>

class CompressThread
{
public:
    CompressThread(int poolSize,long size,int acceleration);
    virtual ~CompressThread();
    void PutImageTask(unsigned char* data,int64_t time);
    void GetImageRes(unsigned char* &data,int32_t &size,int64_t &time);
private:
    class ImageHandle
    {
        public:
            ImageHandle();
            virtual ~ImageHandle();
            void operator()();
            //当图像大小改变时，一定要调用这个初始化
            void InitHandle(long size,int CmpAcceleration);
            void setTask(unsigned char* data,int64_t timeSp);
            void getRes(unsigned char* &data,int32_t &size,int64_t& timeSp);
            void exitThread();
            void SetAcceleration(int value);
        private:
            std::thread runThread;
            std::mutex tlck;
            std::mutex outlck;
            std::condition_variable inCond,outCond;
            unsigned char* doubleBuff[2]={nullptr,nullptr};
            unsigned char* resBuff=nullptr;
            LZ4_stream_t* lz4Stream_body=nullptr;
            std::atomic<bool> exitFlag=false;
            std::atomic<bool> taskReady=false;
            std::atomic<bool> resReady=false;
            long buffPoint=0;
            long rawSize,cmpSize,maxCmpSize;
            int64_t timesp;
            int Acceleration=100;

    };
    ImageHandle imageHandle;
    std::vector<ImageHandle> threadPool;
    std::atomic<bool> exitFlag=false;  //退出的时候堵住图像输入
    int ThreadPoolSize;
    int putPos=0;
    int getPos=0;

    //Debug
    long InCount=0;
    long OutCount=0;
};

#endif
